package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/image")
public class TestController {
    @RequestMapping("/test")
    public String test(){
        return "test";
    }
    @RequestMapping("upload.action")
    @ResponseBody
    public String uploadImage(HttpServletRequest request) throws IOException {
        String imgData = request.getParameter("imgData");
        System.out.println(imgData.getBytes().length);
        //字节处处base64解码
        byte[] byteImageData=null;
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            byteImageData = base64Decoder.decodeBuffer(imgData);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-dd");
        String fileUrl = "D:\\test.png";
        File file = new File(fileUrl);
        file.createNewFile();
        OutputStream out = new FileOutputStream(file);
        out.write(byteImageData);
        out.flush();
        out.close();
        return "";
    }
}
